package com.aiju.cash.ui.member;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.aiju.base.activity.BaseTitleActivity;
import com.aiju.cash.R;
import com.aiju.cash.core.api.DataJson;
import com.aiju.cash.core.api.MemberApi;
import com.aiju.cash.ui.member.entity.Member;
import com.aiju.common.PixValue;
import com.aiju.forexpand.addresspicker.AddressPicker;
import com.aiju.forexpand.datepicker.DatePicker;
import com.aiju.utils.CheckUtil;
import com.aiju.utils.DateUtil;
import com.aiju.utils.KeyBoardUtils;
import com.aiju.utils.L;
import com.aiju.utils.T;
import com.aiju.widgit.layout.SettingItemEditView;
import com.aiju.widgit.layout.SettingItemRadioButton;
import com.aiju.widgit.layout.SettingItemTextView;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;

import static com.aiju.cash.ui.good.GoodAddActivity.REQUEST_CODE;
import static com.aiju.cash.ui.member.MemberRemarkActivity.RESULT_MORE_CODE;
import static com.aiju.cash.ui.member.MemberRemarkActivity.RESULT_REMARK_CODE;

/**
 * @author: 佐为
 * @date: 2017-02-20 15:59
 * @version: V1.0
 * @Description: 会员详情
 */
public class MemberInfoActivity extends BaseTitleActivity implements View.OnClickListener {

    public static final int ADD = 0;
    public static final int INFO = 1;

    private SettingItemEditView nameTextView, phoneTextView;

    private SettingItemTextView birthdayTextView, addressTextView;

    private SettingItemRadioButton sexTextView;

    private SettingItemTextView addressMoreTextView, remarkTextView;

    /**
     * 会员信息
     */
    private Member mMember;

    /**
     * 显示类型
     */
    private int type;

    /**
     * 新建会员的电话号码
     */
    private String phone;


    private String name;

    private String gender = "";
    private String birthday = "";
    private String province = "";
    private String city = "";
    private String area = "";

    private String address = "";
    private String remark = "";
    private String memberId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_member_info);
        setCenterTitle("会员详情");

        nameTextView = (SettingItemEditView) findViewById(R.id.tv_member_name);
        phoneTextView = (SettingItemEditView) findViewById(R.id.tv_member_phone);
        sexTextView = (SettingItemRadioButton) findViewById(R.id.tv_member_sex);
        birthdayTextView = (SettingItemTextView) findViewById(R.id.tv_member_birthday);
        addressTextView = (SettingItemTextView) findViewById(R.id.tv_member_address);
        addressMoreTextView = (SettingItemTextView) findViewById(R.id.tv_member_address_more);
        remarkTextView = (SettingItemTextView) findViewById(R.id.tv_member_address_remark);
        nameTextView.setEditable(true);
        phoneTextView.setEditable(true);

 /*      phoneTextView.setRightInputType(InputType.TYPE_CLASS_PHONE);
        nameTextView.setRightInputType(InputType.TYPE_CLASS_TEXT);
        sexTextView.setRadioGroupCheck(gender);*/
        nameTextView.setOnClickListener(this);
        phoneTextView.setOnClickListener(this);
        birthdayTextView.setOnClickListener(this);
        addressTextView.setOnClickListener(this);
        addressMoreTextView.setOnClickListener(this);
        remarkTextView.setOnClickListener(this);
        addressMoreTextView.getRightView().setPadding(0, PixValue.dp.valueOf(5), 0, PixValue.dp.valueOf(5));
        remarkTextView.getRightView().setPadding(0, PixValue.dp.valueOf(5), 0, PixValue.dp.valueOf(5));

        sexTextView.setEditable(true);
        sexTextView.setChangeListener(new SettingItemRadioButton.OnChangeListener() {
            @Override
            public void change(String item) {
                gender = item;
            }
        });
        showTypeView();
    }

    /**
     * 根据类型显示视图
     */
    private void showTypeView() {
        type = getIntent().getIntExtra("type", 0);
        setRightTitle("完成");
        if (type == INFO) { //会员展示信息
            mMember = (Member) getIntent().getSerializableExtra("member");
            memberId = mMember.getId();
            setMemberInfoView();
        } else { //添加会员详细
            phone = getIntent().getStringExtra("phone");
            memberId = getIntent().getStringExtra("memberId");
            phoneTextView.setRightText(phone);
        }
        mAiJuTitleBarView.setRightOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                requestMemberUpdata();
            }
        });
    }

    /**
     * 修改会员信息
     */
    private void requestMemberUpdata() {
        phone = phoneTextView.getRightText();
        name = nameTextView.getRightText();
        if (TextUtils.isEmpty(name)) {
            this.name = phone;
        } else {
            if (!CheckUtil.checkNickName(name)) {
                T.ToastShort("姓名最多只支持十一位汉字、数字及英文");
                return;
            }
        }


        mDialogHelper.showLoadingProgressDialog();
        new MemberApi().memberUpdate(memberId, mUser.getToken(), name, gender, birthday, province,
                city, area, address, remark, new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        mDialogHelper.dismissProgressDialog();
                        T.ToastNetError();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        mDialogHelper.dismissProgressDialog();
                        DataJson dataJson = new DataJson(response);
                        if (dataJson.isSuccess()) {
                            if (type == INFO) {
                                finish();
                            } else {
                                finish();
                                Intent intent = new Intent(MemberInfoActivity.this, MemberInfoCardsActivity.class);
                                intent.putExtra("memberId", memberId);
                                startActivity(intent);
                            }
                            T.ToastShort("设置会员信息成功");
                        } else {
                            T.ToastShort(dataJson.getMsg());
                        }
                    }
                });
    }

    /**
     * 显示会员信息
     */
    private void setMemberInfoView() {
        nameTextView.setRightText(mMember.getName());
        ((EditText) nameTextView.getRightView()).setSelection(mMember.getName().length());
        phoneTextView.setRightText(mMember.getPhone());
        sexTextView.setRadioGroupCheck(mMember.getGender());
        birthdayTextView.setRightText(mMember.getBirthday());
        addressTextView.setRightText(mMember.getProvince() + mMember.getCity() + mMember.getArea());
        addressMoreTextView.setRightText(mMember.getAddress());
        remarkTextView.setRightText(mMember.getRemark());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_member_address_more:
                goMemberRemark(RESULT_MORE_CODE);
                break;
            case R.id.tv_member_address_remark:
                goMemberRemark(RESULT_REMARK_CODE);
                break;
            case R.id.tv_member_birthday:
                showBirthday();
                break;
            case R.id.tv_member_address:
                showAddress();
                break;
        }

    }

    /**
     * 选择地址
     */
    private void showAddress() {
        AddressPicker addressPicker = new AddressPicker();
        addressPicker.selectAddressDialog(this, addressTextView.getRightView());
        addressPicker.setChangeCity(new AddressPicker.OnChangeCity() {
            @Override
            public void onChangeCity(String pro, String ci, String ar) {
                province = pro;
                city = ci;
                area = ar;
            }
        });
    }

    /**
     * 选择生日
     */
    private void showBirthday() {
        DatePicker dataPicker = new DatePicker(this);
        dataPicker.selectDateDialog(birthdayTextView.getRightView(), DateUtil.getCurrentDate(DateUtil.dateFormatYMD));
        dataPicker.setChangeDate(new DatePicker.OnChangeDate() {
            @Override
            public void onChangeDate(String date) {
                birthday = date;
                L.d(birthday);
            }
        });
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        KeyBoardUtils.closeKeybord(this);
    }

    /**
     * 跳转备注，详细地址
     *
     * @param type
     */
    private void goMemberRemark(int type) {
        Intent intent = new Intent(this, MemberRemarkActivity.class);
        intent.putExtra("type", type);
        if (type == RESULT_MORE_CODE) {
            intent.putExtra("data", address);
        } else {
            intent.putExtra("data", remark);
        }
        startActivityForResult(intent, REQUEST_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE) {
            if (resultCode == RESULT_MORE_CODE) {
                address = data.getStringExtra("second");
                addressMoreTextView.setRightText(address);
            } else {
                remark = data.getStringExtra("second");
                remarkTextView.setRightText(remark);
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
